// ----------------------------------------------------------------------------  
function weather_prefs() {
  this.prefBranch = null;

  // pref values
  this.PREF_LOAD_LOCATION  = "weather.load.location";
  this.PREF_STATION	   = "weather.station";
  this.PREF_UNITS          = "weather.units";
}


// ----------------------------------------------------------------------------  
weather_prefs.prototype.getPrefBranch = function() {
 if (!this.prefBranch){ 
   this.prefBranch = Components.classes['@mozilla.org/preferences-service;1'].getService();
   this.prefBranch = this.prefBranch.QueryInterface(Components.interfaces.nsIPrefBranch);
 }

  return this.prefBranch;
}


// ----------------------------------------------------------------------------  
weather_prefs.prototype.setBoolPref = function(aName, aValue){
  this.getPrefBranch().setBoolPref(aName, aValue);
}


// ----------------------------------------------------------------------------  
weather_prefs.prototype.getBoolPref = function(aName){
  var rv = null;

  try {
    rv = this.getPrefBranch().getBoolPref(aName);
  } catch (e) {}

  return rv;
}

// ----------------------------------------------------------------------------  
weather_prefs.prototype.setIntPref = function(aName, aValue){
  this.getPrefBranch().setIntPref(aName, aValue);
}


// ----------------------------------------------------------------------------
weather_prefs.prototype.getIntPref = function(aName){
  var rv = null;

  try {
    rv = this.getPrefBranch().getIntPref(aName);
  } catch (e) {
  }

  return rv;
}


// ----------------------------------------------------------------------------
weather_prefs.prototype.setCharPref = function(aName, aValue) {
  this.getPrefBranch().setCharPref(aName, aValue);
}


// ----------------------------------------------------------------------------
weather_prefs.prototype.getCharPref = function(aName) {
  var value = null;

  try {
    value = this.getPrefBranch().getCharPref(aName);
  } catch (e) {

  }

  return value;
}


// ------------------------------------------------------------------------------
weather_prefs.prototype.addObserver = function(aDomain, aFunction) {
  var myPrefs = this.getPrefBranch();
  var prefBranchInternal = myPrefs.QueryInterface(Components.interfaces.nsIPrefBranchInternal);

  if (prefBranchInternal) {
    prefBranchInternal.addObserver(aDomain, aFunction, false);
  }
}


// ------------------------------------------------------------------------------
weather_prefs.prototype.removeObserver = function(aDomain, aFunction) {
  var myPrefs = this.getPrefBranch();
  var prefBranchInternal = myPrefs.QueryInterface(Components.interfaces.nsIPrefBranchInternal);

  if (prefBranchInternal) {
    prefBranchInternal.removeObserver(aDomain, aFunction);
  }
}
  
  
// ----------------------------------------------------------------------------  
weather_prefs.prototype.initPrefs = function() {
 var arePrefsUpToDate = this.getCharPref(this.PREF_STATION);
  if (arePrefsUpToDate == null){
    // set prefs if they don't exist
    this.initPref(this.PREF_LOAD_LOCATION, "int" , 1);
    this.initPref(this.PREF_STATION, "char", "Boulder Foothills");
  }

  arePrefsUpToDate = this.getIntPref(this.PREF_UNITS);
  if (arePrefsUpToDate == null) {
    this.initPref(this.PREF_UNITS, "int", 0);    
   }
}


// ----------------------------------------------------------------------------
weather_prefs.prototype.initPref =
  function(aPrefName, aPrefType, aDefaultValue) {
  switch (aPrefType) {
    case "bool" :
      var prefExists = this.getBoolPref(aPrefName);
      if (prefExists == null)
        this.setBoolPref(aPrefName, aDefaultValue);
      break;

    case "int" :
      var prefExists = this.getIntPref(aPrefName);
      if (prefExists == null)
        this.setIntPref(aPrefName, aDefaultValue);
      break;

    case "char" :
      var prefExists = this.getCharPref(aPrefName);
      if (prefExists == null)
        this.setCharPref(aPrefName, aDefaultValue);
      break;
    
   default :
    alert("Unknown type in prefs.js " + aPrefType);
  }
}
    